package cn.nbxz.dao;

import cn.nbxz.domain.Category;
import cn.nbxz.util.JdbcHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CategoryDao implements Dao<Category,Integer> {
    private static final String table = " t_categories " ;
    @Override
    public Category load(Integer id) {
        String SQL="select id , name , position , parent_id from"+table+" where id=?";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL , id ) ;
        helper.destory();
        if( list != null && list.size() == 1 ) {
            Map<String,Object> map = list.get( 0 );
            Category c =   this.cast( map );
            return c ;
        }
        return null;
    }
    public Category load(String name){
        String SQL="select id , name , position , parent_id from"+table+" where name=?";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL , name ) ;
        helper.destory();
        if( list != null && list.size() == 1 ) {
            Map<String,Object> map = list.get( 0 );
            Category c =   this.cast( map );
            return c ;
        }
        return null;
    }

    @Override
    public List<Category> list() {
        String SQL="select id , name , position , parent_id from"+table;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL ) ;
        helper.destory();
        if( list != null && list.size() >0 ) {
            List<Category> categories = new ArrayList<>();
            for( int i = 0 ; i < list.size() ; i++ ) {
                Map<String, Object> map = list.get(i);
                Category c =   this.cast( map );
                categories.add(c);
            }
            return categories;
        }
        return null;
    }

    @Override
    public Category cast(Map<String, Object> map) {
        Category category=new Category();
        category.setId((Integer)map.get("id"));
        category.setName((String)map.get("name"));
        category.setPosition((Integer)map.get("position"));

        Category c= this.load((Integer)map.get("parent_id"));

        category.setParent(c);

        return category;
    }

    @Override
    public boolean save(Category c) {
        String SQL="insert into "+table+" ( name , position , parent_id) "+
                " values(?,?,?) ";
        Category ct=  c.getParent();
        Integer parent_id= ct.getId();

        Object[] array={  c.getName() , c.getPosition() ,parent_id  };
        JdbcHelper helper = new JdbcHelper();
        int id = helper.insert( SQL , array );
        c.setId(id);
        helper.destory();

        return id != -1 ;
    }

    @Override
    public boolean update(Category c) {
        String SQL="update "+table+"set name=?, position=?, parent_id=? "
                +"where id=? ";
        Category cparent=  c.getParent();
        Integer parent_id= cparent.getId();
        Object[] array={c.getName(),c.getPosition(),parent_id,c.getId()};
        JdbcHelper helper = new JdbcHelper();
        int count = helper.update( SQL , array );
        helper.destory();

        return count == 1 ;
    }

    @Override
    public boolean delete(Integer id) {
        String SQL = "DELETE FROM " + table + " WHERE id = ? ";
        JdbcHelper helper = new JdbcHelper();
        int count = helper.update( SQL , id );
        helper.destory();
        return count == 1 ;

    }
}
